#!/bin/bash 

CA_FILE=/opt/config/crypto-config/ordererOrganizations/sample.com/tlsca/tlsca.sample.com-cert.pem
CC_NAME=mycc 
CC_VERSION=1.0

dockerExec() {
    mspid=$1
    org=$2
    peer=$3
    cmd=$4
    echo "$cmd"
    docker exec \
       -e  CORE_PEER_LOCALMSPID=$mspid \
       -e  CORE_PEER_ADDRESS=$peer.$org.sample.com:7051 \
       -e  CORE_PEER_MSPCONFIGPATH=/opt/config/crypto-config/peerOrganizations/$org.sample.com/users/Admin@$org.sample.com/msp \
       -e  CORE_PEER_TLS_ROOTCERT_FILE=/opt/config/crypto-config/peerOrganizations/$org.sample.com/tlsca/tlsca.$org.sample.com-cert.pem \
      -t sample-client $cmd 
    if test $? -ne 0; then 
        echo "fail to exec command"
        exit 1
    fi 
}

ORG1_TLS=/opt/config/crypto-config/peerOrganizations/org1.sample.com/tlsca/tlsca.org1.sample.com-cert.pem
ORG2_TLS=/opt/config/crypto-config/peerOrganizations/org2.sample.com/tlsca/tlsca.org2.sample.com-cert.pem

# create channel 
command="peer channel create -o orderer0.sample.com:7050 --tls --cafile $CA_FILE -f /opt/config/channels/mychannel.tx --channelID mychannel"
dockerExec "Org1MSP" "org1" "peer0" "$command"

# join channel 
command="peer channel join -b mychannel.block"
dockerExec "Org1MSP" "org1" "peer0" "$command"
dockerExec "Org2MSP" "org2" "peer0" "$command"

# install chaincode 
command="peer lifecycle chaincode install /opt/gopath/src/github.com/chaincode/mycc.tar.gz"
dockerExec "Org1MSP" "org1" "peer0" "$command"
sleep 1
dockerExec "Org2MSP" "org2" "peer0" "$command"
sleep 1

# approve chaincode 
PACKAGE_ID="mycc_1:3a4acb156e11f890e4c9bc09a1fb242aafb177aceaa432265772fcee84b67c76"
command="peer lifecycle chaincode approveformyorg -o orderer0.sample.com:7050 --init-required --channelID mychannel --name $CC_NAME --version $CC_VERSION --package-id $PACKAGE_ID --sequence 1 --tls --cafile $CA_FILE"
dockerExec "Org1MSP" "org1" "peer0" "$command"
sleep 1
dockerExec "Org2MSP" "org2" "peer0" "$command"
sleep 1

# check commit 
command="peer lifecycle chaincode checkcommitreadiness --init-required --channelID mychannel --name $CC_NAME --version $CC_VERSION --sequence 1 --tls --cafile $CA_FILE --output json"
dockerExec "Org1MSP" "org1" "peer0" "$command"
sleep 1

# commit chaincode 
command="peer lifecycle chaincode commit -o orderer0.sample.com:7050 --init-required --channelID mychannel --name $CC_NAME --version $CC_VERSION --sequence 1 --tls --cafile $CA_FILE --peerAddresses peer0.org1.sample.com:7051 --tlsRootCertFiles $ORG1_TLS --peerAddresses peer0.org2.sample.com:7051 --tlsRootCertFiles $ORG2_TLS"
dockerExec "Org1MSP" "org1" "peer0" "$command"

# init chaincode 
command="peer chaincode invoke -o orderer0.sample.com:7050 --isInit --tls --cafile $CA_FILE -C mychannel -n $CC_NAME --peerAddresses peer0.org1.sample.com:7051 --tlsRootCertFiles $ORG1_TLS --peerAddresses peer0.org2.sample.com:7051 --tlsRootCertFiles $ORG2_TLS -c {\"Args\":[\"init\",\"a\",\"80000000\",\"b\",\"100\"]}"
dockerExec "Org1MSP" "org1" "peer0" "$command"

# invoke chaincode
command="peer chaincode invoke -o orderer0.sample.com:7050 --tls --cafile $CA_FILE -C mychannel -n $CC_NAME --peerAddresses peer0.org1.sample.com:7051 --tlsRootCertFiles $ORG1_TLS --peerAddresses peer0.org2.sample.com:7051 --tlsRootCertFiles $ORG2_TLS -c {\"Args\":[\"invoke\",\"a\",\"b\",\"100\"]}"
dockerExec "Org1MSP" "org1" "peer0" "$command"
dockerExec "Org2MSP" "org2" "peer0" "$command"

# query chaincode
command="peer chaincode query --channelID mychannel -n $CC_NAME -c {\"Args\":[\"query\",\"a\"]}"
dockerExec "Org1MSP" "org1" "peer0" "$command"
